
#include "74HC573.h"

uint8_t code_distab_cathode[16]={
0x3f,0x06,0x5b,0x4f,0x66,0x6d,0x7d,0x07, 
0x7f,0x6f,0x77,0x7c,0x39,0x5e,0x79,0x71}; //共阴数码管段选码表，无小数点
uint8_t code_distab_cathode_x[16]={
0xbf,0x86,0xdb,0xcf,0xe6,0xed,0xfd,0x87,
0xff,0xef,0xf7,0xfc,0xb9,0xde,0xf9,0xf1}; //共阴数码管段选码表，有小数点
uint8_t code_distab_anode[16]={
0xc0,0xf9,0xa4,0xb0,0x99,0x92,0x82,0xf8,
0x80,0x90,0x88,0x83,0xc6,0xa1,0x86,0x8e};  //共阳数码管段选码表，无小数点
uint8_t code_distab_anode_x[16]={
0x40,0x79,0x24,0x30,0x19,0x12,0x02,0x78,
0x00,0x10,0x08,0x03,0x46,0x21,0x06,0x0e}; //共阳数码管段选码表，有小数点

void	C573Init(void)
{
	GPIO_InitTypeDef GPIO_InitStructure;
	RCC_APB2PeriphClockCmd(HC573_LE_W_CLK,ENABLE);		/*开启端口的时钟*/
	GPIO_InitStructure.GPIO_Pin = HC573_LE_W_PIN; 		//选择引脚		
	GPIO_InitStructure.GPIO_Mode = GPIO_Mode_Out_PP;  /*设置引脚模式*/
	GPIO_InitStructure.GPIO_Speed = GPIO_Speed_2MHz;	/*设置引脚速率 */   
	GPIO_Init(HC573_LE_W_PORT, &GPIO_InitStructure);	/*使用结构体初始化*/
	
	RCC_APB2PeriphClockCmd(HC573_LE_D_CLK,ENABLE);		/*开启端口的时钟*/
	GPIO_InitStructure.GPIO_Pin = HC573_LE_D_PIN; 		//选择引脚		
	GPIO_InitStructure.GPIO_Mode = GPIO_Mode_Out_PP;  /*设置引脚模式*/
	GPIO_InitStructure.GPIO_Speed = GPIO_Speed_2MHz;	/*设置引脚速率 */   
	GPIO_Init(HC573_LE_D_PORT, &GPIO_InitStructure);	/*使用结构体初始化*/
	
	RCC_APB2PeriphClockCmd(HC573_DATA0_CLK,ENABLE);		/*开启端口的时钟*/
	GPIO_InitStructure.GPIO_Pin = HC573_DATA0_PIN; 		//选择引脚		
	GPIO_InitStructure.GPIO_Mode = GPIO_Mode_Out_PP;  /*设置引脚模式*/
	GPIO_InitStructure.GPIO_Speed = GPIO_Speed_2MHz;	/*设置引脚速率 */   
	GPIO_Init(HC573_DATA0_PORT, &GPIO_InitStructure);	/*使用结构体初始化*/
		
	RCC_APB2PeriphClockCmd(HC573_DATA1_CLK,ENABLE);		/*开启端口的时钟*/
	GPIO_InitStructure.GPIO_Pin = HC573_DATA1_PIN; 		//选择引脚		
	GPIO_InitStructure.GPIO_Mode = GPIO_Mode_Out_PP;  /*设置引脚模式*/
	GPIO_InitStructure.GPIO_Speed = GPIO_Speed_2MHz;	/*设置引脚速率 */   
	GPIO_Init(HC573_DATA1_PORT, &GPIO_InitStructure);	/*使用结构体初始化*/
		
	RCC_APB2PeriphClockCmd(HC573_DATA2_CLK,ENABLE);		/*开启端口的时钟*/
	GPIO_InitStructure.GPIO_Pin = HC573_DATA2_PIN; 		//选择引脚		
	GPIO_InitStructure.GPIO_Mode = GPIO_Mode_Out_PP;  /*设置引脚模式*/
	GPIO_InitStructure.GPIO_Speed = GPIO_Speed_2MHz;	/*设置引脚速率 */   
	GPIO_Init(HC573_DATA2_PORT, &GPIO_InitStructure);	/*使用结构体初始化*/

	RCC_APB2PeriphClockCmd(HC573_DATA3_CLK,ENABLE);		/*开启端口的时钟*/
	GPIO_InitStructure.GPIO_Pin = HC573_DATA3_PIN; 		//选择引脚		
	GPIO_InitStructure.GPIO_Mode = GPIO_Mode_Out_PP;  /*设置引脚模式*/
	GPIO_InitStructure.GPIO_Speed = GPIO_Speed_2MHz;	/*设置引脚速率 */   
	GPIO_Init(HC573_DATA3_PORT, &GPIO_InitStructure);	/*使用结构体初始化*/
	
	RCC_APB2PeriphClockCmd(HC573_DATA4_CLK,ENABLE);		/*开启端口的时钟*/
	GPIO_InitStructure.GPIO_Pin = HC573_DATA4_PIN; 		//选择引脚		
	GPIO_InitStructure.GPIO_Mode = GPIO_Mode_Out_PP;  /*设置引脚模式*/
	GPIO_InitStructure.GPIO_Speed = GPIO_Speed_2MHz;	/*设置引脚速率 */   
	GPIO_Init(HC573_DATA4_PORT, &GPIO_InitStructure);	/*使用结构体初始化*/
		
	RCC_APB2PeriphClockCmd(HC573_DATA5_CLK,ENABLE);		/*开启端口的时钟*/
	GPIO_InitStructure.GPIO_Pin = HC573_DATA5_PIN; 		//选择引脚		
	GPIO_InitStructure.GPIO_Mode = GPIO_Mode_Out_PP;  /*设置引脚模式*/
	GPIO_InitStructure.GPIO_Speed = GPIO_Speed_2MHz;	/*设置引脚速率 */   
	GPIO_Init(HC573_DATA5_PORT, &GPIO_InitStructure);	/*使用结构体初始化*/
			
	RCC_APB2PeriphClockCmd(HC573_DATA6_CLK,ENABLE);		/*开启端口的时钟*/
	GPIO_InitStructure.GPIO_Pin = HC573_DATA6_PIN; 		//选择引脚		
	GPIO_InitStructure.GPIO_Mode = GPIO_Mode_Out_PP;  /*设置引脚模式*/
	GPIO_InitStructure.GPIO_Speed = GPIO_Speed_2MHz;	/*设置引脚速率 */   
	GPIO_Init(HC573_DATA6_PORT, &GPIO_InitStructure);	/*使用结构体初始化*/
	
	RCC_APB2PeriphClockCmd(HC573_DATA7_CLK,ENABLE);		/*开启端口的时钟*/
	GPIO_InitStructure.GPIO_Pin = HC573_DATA7_PIN; 		//选择引脚		
	GPIO_InitStructure.GPIO_Mode = GPIO_Mode_Out_PP;  /*设置引脚模式*/
	GPIO_InitStructure.GPIO_Speed = GPIO_Speed_2MHz;	/*设置引脚速率 */   
	GPIO_Init(HC573_DATA7_PORT, &GPIO_InitStructure);	/*使用结构体初始化*/
	
	RCC_APB2PeriphClockCmd(HC573_CSAD_CLK,ENABLE);		/*开启端口的时钟*/
	GPIO_InitStructure.GPIO_Pin = HC573_CSAD_PIN; 		//选择引脚		
	GPIO_InitStructure.GPIO_Mode = GPIO_Mode_IN_FLOATING;  /*设置引脚模式*/
//	GPIO_InitStructure.GPIO_Speed = GPIO_Speed_2MHz;	/*设置引脚速率 */   
	GPIO_Init(HC573_CSAD_PORT, &GPIO_InitStructure);	/*使用结构体初始化*/

//初始化引脚	
	HC573_LE_D_L();
	HC573_LE_W_L();
	HC573_DATA0_H();
	HC573_DATA1_H();
	HC573_DATA2_H();
	HC573_DATA3_H();
	HC573_DATA4_H();
	HC573_DATA5_H();
	HC573_DATA6_H();
	HC573_DATA7_H();

};
//位码函数
void	HC573_W_code(u8 bit_data )
{
	HC573_DATA0_H();
	HC573_DATA1_H();
	HC573_DATA2_H();
	HC573_DATA3_H();
	HC573_DATA4_H();
	HC573_DATA5_H();
	HC573_DATA6_H();
	HC573_DATA7_H();
	switch(bit_data){
//		case 1:
//			HC573_DATA7_L();
//			break;
//		case 2:
//			HC573_DATA6_L();
//			break;
		case 1:
			HC573_DATA5_L();
			break;
		case 2:
			HC573_DATA4_L();
			break;
		case 3:
			HC573_DATA3_L();
			break;
		case 4:
			HC573_DATA2_L();
			break;
		case 5:
			HC573_DATA1_L();
			break;
		case 6:
			HC573_DATA0_L();
			break;											
	}
};
//断码函数
void 	HC573_D_code(u8 blo_data )
{
	if(blo_data&0x01){
		HC573_DATA0_H();
	}else{
		HC573_DATA0_L();
	}
	if(blo_data&0x02){
		HC573_DATA1_H();
	}else{
		HC573_DATA1_L();
	}
	if(blo_data&0x04){
		HC573_DATA2_H();
	}else{
		HC573_DATA2_L();
	}
	if(blo_data&0x08){
		HC573_DATA3_H();
	}else{
		HC573_DATA3_L();
	}
	if(blo_data&0x10){
		HC573_DATA4_H();
	}else{
		HC573_DATA4_L();
	}
	if(blo_data&0x20){
		HC573_DATA5_H();
	}else{
		HC573_DATA5_L();
	}
	if(blo_data&0x40){
		HC573_DATA6_H();
	}else{
		HC573_DATA6_L();
	}	
	if(blo_data&0x80){
		HC573_DATA7_H();
	}else{
		HC573_DATA7_L();
	}
	
};
//显示函数纯数字
void	HC573_DPY_num(int32_t num)
{
	u8 wei,i,blo;
	int32_t numin;
	numin = num;
	if(num<10)//判断有几位要显示   
		wei=1;
	else if(num<100)
		wei=2;
	else if(num<1000)
		wei=3;
	else if(num<10000)
		wei=4;
	else if(num<100000)
		wei=5;
	else if(num<1000000)
		wei=6;
	else if(num<10000000)
		wei=7;
	else
		wei=8;
	for(i=1;i<=wei;i++)//显示 
	{
		HC573_W_code( i );
		HC573_LE_W_H();
		Delay(1);
		HC573_LE_W_L();
		blo = numin%10;
		HC573_D_code( code_distab_cathode[blo] );
		HC573_LE_D_H();
		Delay(1);
		HC573_LE_D_L();
		numin /= 10;
		Delay(1);
		HC573_D_code(0x00);
		HC573_LE_D_H();
		Delay(1);
		HC573_LE_D_L();
	}
};

//显示函数 消费金额 最小单位（分）
/*
num  :金额
decimal_len ：
*/
void	HC573_DPY_num_decimal(int32_t num,uint8_t decimal_len)
{
	u8 wei,i,blo;
	int32_t numin;
	numin = num;
	if(num<10)//判断有几位要显示   
		wei=1;
	else if(num<100)
		wei=2;
	else if(num<1000)
		wei=3;
	else if(num<10000)
		wei=4;
	else if(num<100000)
		wei=5;
	else if(num<1000000)
		wei=6;
	else if(num<10000000)
		wei=7;

	if(wei > 6) return;   //只能显示6位数字
	
	for(i=1;i<=(wei<=2?(decimal_len+1):wei);i++)//显示 
	{
		HC573_W_code( i );
		HC573_LE_W_H();
		Delay(1);
		HC573_LE_W_L();
		blo = numin%10;
		if( i != decimal_len+1 ){
			HC573_D_code(code_distab_cathode[blo]);//无小数点
		}else{
			HC573_D_code(code_distab_cathode_x[blo]);//小数点
		}
		HC573_LE_D_H();
		Delay(1);
		HC573_LE_D_L();
		numin /= 10;
		Delay(1);
		HC573_D_code(0x00);
		HC573_LE_D_H();
		Delay(1);
		HC573_LE_D_L();
	}
	
};

//显示函数——字符
void	HC573_DPY_str(const char *str,uint8_t len)
{
	uint8_t i = 6;
	uint32_t tmpResult = 0;  
	while ( len-- > 0 )  
	{  
			if (*str >= '0' && *str <= '9')  
			{  
					tmpResult = *str - '0';  
			}  
			else if(*str >= 'A' && *str <= 'F')  
			{  
					tmpResult = *str - 'A' + 10;  
			}  
			else if(*str >= 'a' && *str <= 'f')  
			{  
					tmpResult = *str - 'a' + 10;  
			}  
			str++;  
			//显示字符
			HC573_W_code( i-- );
			HC573_LE_W_H();
			Delay(1);
			HC573_LE_W_L();
			HC573_D_code( code_distab_cathode[tmpResult] );//字符所在的数组编码
			HC573_LE_D_H();
			Delay(1);
			HC573_LE_D_L();
			Delay(1);
			HC573_D_code(0x00);
			HC573_LE_D_H();
			Delay(1);
			HC573_LE_D_L();
	}
};
//显示到计时函数
void	HC573_DPY_run(void)
{
	static	uint8_t num=0;
		HC573_DATA0_L();
		HC573_DATA1_L();
		HC573_DATA2_L();
		HC573_DATA3_L();
		HC573_DATA4_L();
		HC573_DATA5_L();
		HC573_DATA6_H();
		HC573_DATA7_H();	
		HC573_LE_W_H();
		delay_ms(500);
		HC573_LE_W_L();	
		HC573_D_code( code_distab_cathode[num++] );	
		HC573_LE_D_H();
		delay_ms(500);
		HC573_LE_D_L();
	if(num>9)num = 0;
}


